package com.jy.adscm.operation.mscyb.controller;


import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.JqbzAssembly;
import com.jy.adscm.operation.mscyb.service.JqbzAssemblyService;
import com.jy.adscm.operation.mscyb.service.JqbzAssemblyService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *装配记录
 * @author zyl
 * @since 2019-05-25
 */
@RestController
@RequestMapping("/adscm/operation/jqbz/jqbzAssembly")
public class JqbzAssemblyController {
    @Autowired
    JqbzAssemblyService jqbzAssemblyService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;

    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody JqbzAssembly jqbzAssembly, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        jqbzAssembly.setCreator(token.getUser().getId().toString());
        jqbzAssembly.setCreatTime(new Date());
        boolean insert = jqbzAssemblyService.insert(jqbzAssembly);
        return new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody List<JqbzAssembly> jqbzAssemblys, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        for (JqbzAssembly jqbzAssembly : jqbzAssemblys) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzAssembly.getValue(),null)){
                jqbzAssembly.setCreator(token.getUser().getId().toString());
                jqbzAssembly.setCreatTime(new Date());
                boolean result = jqbzAssemblyService.updateById(jqbzAssembly);
            }
        }
        return new SimpleCoreResponse("200","修改成功",true);
    }
    @RequestMapping("selectByPid/{pid}")
    public SimpleCoreResponse selectByPid(@PathVariable String pid, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        List<JqbzAssembly> jqbzMaterialListChecks=jqbzAssemblyService.selectByPid(pid);
        return new SimpleCoreResponse("200","查询成功",jqbzMaterialListChecks);
    }
}

